package com.test.cordova.view;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.view.KeyEvent;

import com.test.cordova.R;


/**
 * 加载中提示.
 */

public class LoadingDialog {


    private static Dialog loadDialog;

    private static DialogInterface.OnKeyListener keylistener = new DialogInterface.OnKeyListener() {
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                return true;
            } else {
                return false;
            }
        }
    };

    private static void createDialog(@NonNull Context context) {
        loadDialog = new Dialog(context, android.R.style.Theme_Translucent_NoTitleBar);
        loadDialog.setContentView(R.layout.dialog_loading);
        //点击对话框外面,对话框不消失
        loadDialog.setCanceledOnTouchOutside(false);
        loadDialog.setOnKeyListener(keylistener);
        loadDialog.setCancelable(false);
    }

    public static void show(Context context){
        if (loadDialog == null) {
            createDialog(context);
        }
        if (!loadDialog.isShowing()) {
            loadDialog.show();
        }
    }

    public static void dismiss(){
        if (loadDialog != null) {
            if (loadDialog.isShowing()) {
                loadDialog.cancel();
                loadDialog.dismiss();
                loadDialog = null;
            }
        }
    }
}
